package com.hulytu.android.adapter.listview;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.hulytu.android.adapter.R;
import com.hulytu.android.adapter.ViewAdapter;

public abstract class BasicViewAdapter<VH extends BasicViewHolder> extends BaseAdapter implements ViewAdapter<VH> {

    private static final int VIEW_TAG_KEY = R.id.tag_adapter_convert_view;

    @Override
    public int getCount() {
        return getItemCount();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Object tag;
        VH holder;
        if (convertView != null && (tag = convertView.getTag(VIEW_TAG_KEY)) != null) {
            holder = (VH) tag;
        } else {
            // 创建 convertView holder
            holder = onCreateViewHolder(parent, getItemViewType(position));
            holder.getItemView().setTag(VIEW_TAG_KEY, holder);
        }

        holder.setAdapterPosition(position);

        render(holder, position);

        return holder.getItemView();
    }

    @Override
    public void notifyItemChanged(int position) {
        notifyDataSetChanged();
    }

    @Override
    public void notifyItemRangeChanged(int positionStart, int itemCount) {
        notifyDataSetChanged();
    }

    @Override
    public void notifyItemInserted(int position) {
        notifyDataSetChanged();
    }

    @Override
    public void notifyItemRangeInserted(int positionStart, int itemCount) {
        notifyDataSetChanged();
    }

    @Override
    public void notifyItemRemoved(int position) {
        notifyDataSetChanged();
    }

    @Override
    public void notifyItemRangeRemoved(int positionStart, int itemCount) {
        notifyDataSetChanged();
    }
}
